package com.itheima;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;

@WebServlet("/servletDemo2")
public class ServletDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String cj = request.getParameter("cj");
        ServletContext servletContext = getServletContext();
        String realPath = servletContext.getRealPath("stu.txt");
        FileOutputStream os=new FileOutputStream(realPath,true);
        String s1=name+","+age+","+cj;
        os.write(s1.getBytes());
        os.write(System.lineSeparator().getBytes());
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.getWriter().write("添加成功,两秒后跳转到首页");
        response.setHeader("Refresh","3;URL=/day08h/stu.html");
        os.close();
    }
}